package com.stock.stockana.ui.fragments;

import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;

import com.stock.stockana.R;
import com.stock.stockana.beans.StockInfoBean;
import com.stock.stockana.events.StockListChangedEvent;
import com.stock.stockana.ui.activities.StockDetailActivity;
import com.stock.stockana.ui.adapter.CollectStockAdapter;
import com.stock.stockana.ui.listener.OnStockItemClickedListener;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * ---------------------------------------------------
 * Description: 查看已收藏股票的fragment页面适配器
 * Author: ljgsonx
 * Belong to: com.stock.stockana.fragments
 * Time: 2016/1/20 18:41
 * ---------------------------------------------------
 */
public class CollectStockFragment extends Fragment {

    private final static String TAG = "CollectStockFragment";
    private View mView;
    private RecyclerView mRecyclerView;
    private Button mWakeUpSettedBtn;
    private Button mWakeUpNotSetBtn;
    private RecyclerView.LayoutManager mLayoutManager;
    private CollectStockAdapter mCollectStockAdapter;
    private LinearLayout mFooter;

    private View.OnTouchListener mCollectStockTouchListener = new View.OnTouchListener() {
        private static final int SWIPE_MIN_DISTANCE = 50;
        private static final int SWIPE_MAX_OFF_PATH = 250;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;
        private float mDownX = -1;
        private float mDownY = -1;
        private int moveNums = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_MOVE:
                    if(moveNums == 0) {
                        mDownX = event.getX();
                        mDownY = event.getY();
                    }
                    moveNums++;
                    break;
                case MotionEvent.ACTION_UP:
                    if(mDownX >=0 && mDownY >= 0) {
                        onFling(mDownX, mDownY, event.getX(), event.getY());
                    }
                    mDownX = mDownY = -1;
                    moveNums = 0;
                    break;
                default:break;
            }
            return false;
        }

        public void onFling(float downX,float downY,float upX,float upY){
            if (Math.abs(upX - downX) > SWIPE_MAX_OFF_PATH) {
                return;
            }
            //从下到上
            if (downY - upY > SWIPE_MIN_DISTANCE) {
                if (mFooter.getVisibility() != View.GONE) {
                    if (CollectStockAdapter.mCollectStocks.size() > 6) {
                        Animation animation = new TranslateAnimation(0, 0, 0, mFooter.getMeasuredHeight());
                        animation.setDuration(300);
                        mFooter.startAnimation(animation);
                        mFooter.setVisibility(View.GONE);
                    }
                }
                //从上到下
            } else if (upY - downY > SWIPE_MIN_DISTANCE) {
                if (mFooter.getVisibility() == View.GONE) {
                    Animation animation = new TranslateAnimation(0, 0, mFooter.getMeasuredHeight(), 0);
                    animation.setDuration(300);
                    mFooter.startAnimation(animation);
                    mFooter.setVisibility(View.VISIBLE);
                }
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.recyclerview_footer, container, false);
        configViews();
        return mView;
    }

    private void configViews() {
        mRecyclerView = (RecyclerView) mView.findViewById(R.id.id_recyclerview);
        mFooter = (LinearLayout) mView.findViewById(R.id.collectFooter);
        mWakeUpSettedBtn = (Button) mView.findViewById(R.id.settedWakeUpBtn);
        mWakeUpNotSetBtn = (Button) mView.findViewById(R.id.notSetWakeUpBtn);
        mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mCollectStockAdapter = new CollectStockAdapter(getActivity());
        mCollectStockAdapter.setmOnStockItemClickedListener(new OnStockItemClickedListener() {
            @Override
            public void onStockItemCliked(StockInfoBean clickedStock) {
                Intent intent = new Intent(getActivity(), StockDetailActivity.class);
                intent.putExtra("clickedStock", clickedStock);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_from_left);
            }
        });
        mWakeUpSettedBtn.setSelected(true);
        mWakeUpNotSetBtn.setSelected(false);
        mWakeUpSettedBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWakeUpSettedBtn.setSelected(true);
                mWakeUpNotSetBtn.setSelected(false);
                CollectStockAdapter.isSetted = true;
                CollectStockAdapter.updateSettedStocks();
                mCollectStockAdapter.notifyDataSetChanged();
            }
        });
        mWakeUpNotSetBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWakeUpSettedBtn.setSelected(false);
                mWakeUpNotSetBtn.setSelected(true);
                CollectStockAdapter.isSetted = false;
                CollectStockAdapter.updateSettedStocks();
                mCollectStockAdapter.notifyDataSetChanged();
            }
        });
        mRecyclerView.setAdapter(mCollectStockAdapter);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setOnTouchListener(mCollectStockTouchListener);
    }

    @Override
    public void onResume() {
        super.onResume();
        mWakeUpSettedBtn.setSelected(CollectStockAdapter.isSetted);
        mWakeUpNotSetBtn.setSelected(!CollectStockAdapter.isSetted);
        CollectStockAdapter.updateSettedStocks();
        mCollectStockAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * Description: 自选列表产生变化,刷新适配器
     * Author: ljgsonx
     * Time: 2016/1/23 18:57
     */
    @Subscribe
    public void onEventMainThread(StockListChangedEvent event){
        CollectStockAdapter.updateSettedStocks();
        mCollectStockAdapter.notifyDataSetChanged();
    }
}
